package com.datastore.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class DatastoreSample implements EntryPoint {
  final VLayout mainLayout = new VLayout();
  final HLayout buttonLayout = new HLayout();
  
  final ListGrid accountListGrid = new ListGrid();
  
  final DynamicForm accountForm = new DynamicForm();
  
  /**
   * The message displayed to the user when the server cannot be reached or
   * returns an error.
   */
  final DataStoreServiceAsync dssa = GWT.create(DataStoreService.class);
  public void onModuleLoad() {
    final VLayout mainLayout = new VLayout();
    final HLayout buttonLayout = new HLayout();
    
    final ListGrid accountListGrid = new ListGrid();
    
    final DynamicForm accountForm = new DynamicForm();
    
    ListGridField userIdField = new ListGridField("userid", "ID");
    ListGridField userNameField = new ListGridField("username", "User Name");
    ListGridField passwordField = new ListGridField("password","Password");
    ListGridField dateField = new ListGridField("date", "Date");
    
    dssa.getAccountData(new AsyncCallback<Account[]>() {

      @Override
      public void onFailure(Throwable caught) {
        // TODO Auto-generated method stub
        Window.alert("Getting account data failed !");
      }

      @Override
      public void onSuccess(Account[] result) {
        // TODO Auto-generated method stub
        getAccountsData(result);
      }
    });
    
    accountListGrid.setWidth100();
    accountListGrid.setFields(userIdField,userNameField,passwordField,dateField);
    
    TextItem idItem = new TextItem("userid", "ID");
    idItem.setRequired(true);
    idItem.setDisabled(true);
    final TextItem userNameItem = new TextItem("username", "User Name");
    userNameItem.setRequired(true);
    final TextItem passwordItem = new TextItem("password","Password");
    passwordItem.setRequired(true);
    
    
    accountForm.setWidth100();
    accountForm.setFields(idItem,userNameItem,passwordItem);
    
    IButton saveBt = new IButton("Save");
    IButton deleteBt = new IButton("Delete");
    
    buttonLayout.setMembers(saveBt,deleteBt);
    buttonLayout.setMembersMargin(10);
    mainLayout.setMembers(accountListGrid,accountForm,buttonLayout);
    mainLayout.setWidth(800);
    mainLayout.setHeight(500);
    
    RootPanel.get("body").add(mainLayout);
    
    saveBt.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
      
      @Override
      public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
        // TODO Auto-generated method stub
        dssa.StoringData(userNameItem.getValueAsString(), passwordItem.getValueAsString(), new AsyncCallback<Void>() {

          @Override
          public void onFailure(Throwable caught) {
            // TODO Auto-generated method stub
            Window.alert("Stored Failed !");
          }

          @Override
          public void onSuccess(Void result) {
            // TODO Auto-generated method stub
            Window.alert("Stored Success !");
            dssa.getAccountData(new AsyncCallback<Account[]>() {

              @Override
              public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
                Window.alert("Getting account data failed !");
              }

              @Override
              public void onSuccess(Account[] result) {
                // TODO Auto-generated method stub
                getAccountsData(result);
              }
            });
          }
        });
      }
    });
    
  }
  // set up data for the listGrid records
  public void getAccountsData(Account[] result){
              ListGridRecord[] records = new ListGridRecord[result.length];
              for(int i = 0 ; i < result.length; ++i){
                  records[i] = new ListGridRecord();
                  records[i].setAttribute("username", result[i].getUsername());
                  records[i].setAttribute("password",result[i].getPassword());
                  records[i].setAttribute("date", result[i].getDate());
                 
              }
              accountListGrid.setData(records);
  }
}
